package breakout2014;

import com.jme3.app.SimpleApplication;
import com.jme3.material.Material;
import com.jme3.material.RenderState;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.texture.Texture2D;
import com.jme3.ui.Picture;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Logger;
import java.lang.reflect.*;
import java.util.logging.Level;


public class SpatialFactory {
    private static final Logger logger = Logger.getLogger(SpatialFactory.class.getName());
    private SimpleApplication context;
    private final String[] brickList = {"brickSimple", "brickMultiHit"};
    

    public SpatialFactory(SimpleApplication context){
        this.context = context;
    }

    public Spatial buildSpatial(String type, String fileName){
        if (type.matches("background")) {
            return getRawSpatial(type, fileName);
        }
        
        logger.log(Level.WARNING, "Error: Spatial name not found: ", new Object[]{type});
        return null;
    }

    public Spatial buildSpatial(String type){
        if (type.matches("brickSimple")) {
            return getRawSpatial(type, "Textures/brick.png");
        }
        
        logger.log(Level.WARNING, "Error: Spatial name not found: ", new Object[]{type});
        return null;
    }
    
    private Spatial getRawSpatial(String typeName, String fileName) {
        Node node = new Node(typeName);
        Picture pic = new Picture(typeName);
        Texture2D tex = (Texture2D) context.getAssetManager().loadTexture(fileName);
        pic.setTexture(context.getAssetManager(),tex,true);
        
//        adjust picture
        float width = tex.getImage().getWidth();
        float height = tex.getImage().getHeight();
        pic.setWidth(width);
        pic.setHeight(height);
        pic.move(-width/2f,-height/2f,0);
        
//        add a material to the picture
        Material picMat = new Material(context.getAssetManager(), "Common/MatDefs/Gui/Gui.j3md");
        picMat.getAdditionalRenderState().setBlendMode(RenderState.BlendMode.AlphaAdditive);
        node.setMaterial(new Material());
        node.attachChild(pic);
        return node;
    }
}
